using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using DynamicDNS.Provider;

namespace DynamicDNSExample
{
    public partial class MainForm : Form
    {
        private Dictionary<string, IDynamicDNSProvider> tcpProvider = new Dictionary<string, IDynamicDNSProvider>();
        private Dictionary<string, IDynamicDNSProvider> webProvider = new Dictionary<string, IDynamicDNSProvider>();

        public MainForm()
        {
            InitializeComponent();

            InitializeTCPProvider();
            InitializeWebProvider();
            
            cmbProvider.DataSource = new BindingSource(tcpProvider.Keys,null);
        }

        private void InitializeWebProvider()
        {
            webProvider.Add("DynDNS.org", new DynamicDNS.Provider.Web.DynDNSProvider());
            webProvider.Add("MyIP", new DynamicDNS.Provider.Web.MyIPProvider());
        }

        private void InitializeTCPProvider()
        {
            tcpProvider.Add("ChangeIP", new DynamicDNS.Provider.TCP.ChangeIPProvider());
            tcpProvider.Add("DtDNS", new DynamicDNS.Provider.TCP.DtDNSProvider());
            tcpProvider.Add("DynDNS.org", new DynamicDNS.Provider.TCP.DynDNSProvider());
            tcpProvider.Add("NoIP", new DynamicDNS.Provider.TCP.NOIPProvider());
        }

        private void UpdateIP_Click(object sender, EventArgs e)
        {
            if (!CheckPreferences())
            {
                MessageBox.Show("Not all required fields are filled!");
                return;
            }

            ResultForm resultForm = new ResultForm();
            resultForm.UserName = txtUser.Text;
            resultForm.Password = txtPassword.Text;
            resultForm.Host = txtHost.Text;
            if (rdWeb.Checked)
            {
                resultForm.Provider = webProvider[cmbProvider.SelectedValue.ToString()];
            }
            else
            {
                resultForm.Provider = tcpProvider[cmbProvider.SelectedValue.ToString()];
            }
            resultForm.UpdateDNSEntry();
        }

        private bool CheckPreferences()
        {
            if (txtUser.Text == "" || txtPassword.Text == "" || cmbProvider.SelectedItem == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void rd_CheckedChanged(object sender, EventArgs e)
        {
            if (rdWeb.Checked)
            {
                cmbProvider.DataSource = new BindingSource(webProvider.Keys, null);
            }
            else
            {
                cmbProvider.DataSource = new BindingSource(tcpProvider.Keys, null);
            }
        }
    }
}